package saucaca.fragment;

import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.HomeData;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class FragmentErrorConnect extends Fragment {
	MainActivity mActivity;
	TextView textContent;
	Button buttonReconnect;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		getActivity().getActionBar().hide();
		View view = inflater.inflate(R.layout.error_internet, container, false);
		findViewById(view);
		new getService().execute("");
		mActivity.myActionBar.setMenu("", false, true);
		return view;
	}
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		mActivity.myActionBar.setMenu("", false, true);
	}

	public void findViewById(View view) {
		buttonReconnect = (Button) view.findViewById(R.id.button_error_reconnect);
		buttonReconnect.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				new getService().execute("");
			}
		});
	}

	private class getService extends AsyncTask<String, Void, String> {
		final String TAG = "JsonReader";
		private ProgressDialog progressDialog = new ProgressDialog(getActivity());

		public getService() {
			progressDialog.setIndeterminate(true);
		}

		@Override
		protected String doInBackground(String... urls) {
			String str = null;
			str = WServiceLib.getHomeString();
//			BaseEngine.showLogformLongString(str);
			return str;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

			if (!progressDialog.isShowing()) {
				progressDialog.setMessage("Đang kết nối lại!");
				progressDialog.show();
			}
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null) {
				try {
					JsonReader.readHomeData(result);
					/*
					 * mActivity.runOnUiThread(new Runnable() {
					 * 
					 * @Override public void run() { });
					 */
				} catch (Exception e) {
					Log.e(TAG, "_____Exception_______:" + e.toString());
				}
				if (progressDialog.isShowing())
					progressDialog.hide();
				if (HomeData.homeListMENU.size() > 0) {
					mActivity.menuFragment.SetmenuOnline();
					// mActivity.mFragmentList.clear();
					mActivity.showprevousFragment();
				}
			} else {
				if (progressDialog.isShowing())
					progressDialog.dismiss();
			}
		}
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			mActivity = (MainActivity) activity;
		}
	}
}
